﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.events.*;
    import Engine.Events.*;
    import Engine.Managers.*;
    import flash.geom.*;
    import Events.*;
    import Display.DialogUI.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import Classes.virals.*;
    import GameMode.*;
    import Classes.actions.*;
    import Modules.stats.types.*;

    public class HolidayTree extends Decoration {

        public static const PRE_HOLIDAY:String = "pre";
        public static const HOLIDAY:String = "holiday";
        public static const HOLIDAY_WARNING:String = "holiday_warning";
        public static const POST_HOLIDAY:String = "post";
        public static const STATE_LARGETREE_LARGEGIFTS:String = "largetree_largegifts";
        public static const STATE_LARGETREE_MEDIUMGIFTS:String = "largetree_mediumgifts";
        public static const STATE_LARGETREE_SMALLGIFTS:String = "largetree_smallgifts";
        public static const STATE_LARGETREE_LASTGIFT:String = "largetree_lastgift";
        public static const STATE_LARGETREE_NOGIFTS:String = "largetree_nogifts";
        public static const NOGIFTS:int = -1;
        public static const LASTGIFT:int = 1;
        public static const SMALLGIFTS:int = 2;
        public static const MEDIUMGIFTS:int = 3;
        public static const LARGEGIFTS:int = 4;
        public static const LARGETREE:int = 3;

        public static var m_PlacedFromInventory:Boolean = false;
        public static var m_transactionMostPresents:int = 0;
        public static var m_transactionLastPresents:int = 0;
        public static var m_Instance:HolidayTree = null;

        private const HOLIDAYTREE:String = "holidaytree";

        private var m_isHarvesting:Boolean = false;
        public var m_mostPresents:int = 0;
        private var m_lastPresents:int = 0;

        public function HolidayTree(_arg1:String=null){
            super(_arg1);
            m_typeName = this.HOLIDAYTREE;
            m_isHighlightable = true;
            m_Instance = this;
        }
        public static function get instance():HolidayTree{
            if (m_Instance == null){
                m_Instance = new HolidayTree(Item.HOLIDAY2010_TREE_ITEM);
            };
            return (m_Instance);
        }
        public static function doPresentViral():void{
            var _local1:String = ZLoc.t("Dialogs", "Holiday2010_giftrequest_message");
            var _local2 = "Holiday2010_giftrequest";
            var _local3 = "assets/missions/gift.png";
            var _local4:int = GenericDialogView.TYPE_ASKFRIEND;
            HolidayTree.instance.trackAction("present_request");
            UI.displayViralShareMessage(ViralType.HOLIDAY2010_TREE_GIFT_REQUEST, _local1, _local4, onViralDialogClosed, _local2, true, _local3, _local2);
        }
        private static function onViralDialogClosed(_arg1:GenericPopupEvent):void{
            var _local2:Boolean;
            var _local3:String;
            _arg1.target.removeEventListener(GenericPopupEvent.SELECTED, onViralDialogClosed);
            if (_arg1.button != GenericPopup.YES){
                return;
            };
            _local2 = Global.world.viralMgr.sendHoliday2010PresentRequest(Global.player);
            if (_local2 == false){
                _local3 = ZLoc.t("Dialogs", "Holiday2010_alreadyAsked_message");
                UI.displayMessage(_local3, GenericDialogView.TYPE_OK, null, "holidayTreeAlreadyAsked", false, null, "Holiday2010_alreadyAsked");
            };
        }
        public static function showUniquePresentDialog(_arg1:String):void{
            var _local2:String = ZLoc.t("Items", (_arg1 + "_friendlyName"));
            var _local3:String = ZLoc.t("Dialogs", "Holiday2010_unwrap_text");
            var _local4 = "OkButton";
            var _local5 = "Holiday2010_unwrap";
            var _local6 = "";
            var _local7:Item = Global.gameSettings.getItemByName(_arg1);
            if (_local7 == null){
                _local6 = "assets/dialogs/blue_present.png";
            } else {
                _local6 = _local7.unwrapIcon;
            };
            UI.displayMessageWithBold(_local3, _local2, GenericPopup.TYPE_OK, null, _local5, true, _local6, _local5);
        }
        public static function doHolidayStartDialog():void{
            if (m_Instance == null){
                return;
            };
            HolidayTree.instance.trackAction("holiday_start_event");
            var _local1:String = ZLoc.t("Dialogs", "Holiday2010_holidaystart_message");
            var _local2 = "OkButton";
            var _local3 = "Holiday2010_holidaystart";
            var _local4 = "assets/dialogs/holidaytree/treepresents.png";
            UI.displayMessage(_local1, GenericPopup.TYPE_OK, null, _local3, true, _local4, _local3);
        }
        public static function doSpecialMessage():void{
            HolidayTree.instance.trackAction("special_message");
            var _local1:String = ZLoc.t("Dialogs", "HappyHolidays2010_message");
            var _local2 = "OkButton";
            var _local3 = "HappyHolidays2010";
            var _local4 = "assets/dialogs/feed_viral_holiday_santasworkshop_large.png";
            UI.displayMessage(_local1, GenericPopup.TYPE_OK, null, _local3, true, _local4, _local3);
        }
        public static function doPresentExpireWarning():void{
            HolidayTree.instance.trackAction("presents_expiration_warning");
            var _local1:String = ZLoc.t("Dialogs", "Holiday2010_giftswarning_message");
            var _local2 = "OkButton";
            var _local3 = "Holiday2010_giftswarning";
            var _local4 = "assets/missions/gift.png";
            UI.displayMessage(_local1, GenericPopup.TYPE_OK, null, _local3, true, _local4, _local3);
        }
        public static function doPresentExpiredNotice():void{
            HolidayTree.instance.trackAction("presents_expired");
            var _local1:String = ZLoc.t("Dialogs", "Holiday2010_giftsremoved_message");
            var _local2 = "OkButton";
            var _local3 = "Holiday2010_giftsremoved";
            var _local4 = "assets/missions/gift.png";
            UI.displayMessage(_local1, GenericPopup.TYPE_OK, null, _local3, true, _local4, _local3);
        }
        private static function showPreHolidayTreeReminder():void{
            var _local1:String = ZLoc.t("Dialogs", "Holiday2010_treeplacereminder_message");
            var _local2 = "OkButton";
            var _local3:String = ZLoc.t("Dialogs", "Holiday2010_treeplacereminder_title");
            var _local4 = "assets/dialogs/feed_placetree.png";
            var _local5:GenericDialog = new GenericPictureDialog(_local1, _local3, GenericDialogView.TYPE_OK, null, _local3, "", true, 0, _local2, _local4);
            UI.displayPopup(_local5);
        }
        private static function showPreHolidayGiftReminder():void{
            var _local1:String = ZLoc.t("Dialogs", "Holiday2010_sendgiftreminder_message");
            var _local2 = "Holiday2010_sendgiftreminder_button";
            var _local3:String = ZLoc.t("Dialogs", "Holiday2010_sendgiftreminder_title");
            var _local4 = "assets/dialogs/feed_presents.png";
            var _local5:GenericDialog = new GenericPictureDialog(_local1, _local3, GenericDialogView.TYPE_OK, onGiftReminderDialogClosed, _local3, "", true, 0, _local2, _local4);
            UI.displayPopup(_local5);
        }
        private static function onGiftReminderDialogClosed(_arg1:GenericPopupEvent):void{
            _arg1.target.removeEventListener(GenericPopupEvent.SELECTED, onGiftReminderDialogClosed);
            if (_arg1.button != GenericPopup.YES){
                return;
            };
            FrameManager.navigateTo("gifts.php?ref=idle_popup");
        }
        private static function showPreHolidayReminder():void{
            var _local1:Array = Global.world.getObjectsByClass(HolidayTree);
            if (_local1.length == 0){
                showPreHolidayTreeReminder();
            } else {
                showPreHolidayGiftReminder();
            };
        }
        public static function doPreHolidayStartupChecks(_arg1:Boolean=true):void{
            var _local3:Array;
            if (Global.player.inventory == null){
                return;
            };
            var _local2:int = Global.player.inventory.getItemCountByName(Item.HOLIDAY2010_PRESENT_ITEM);
            if (TimestampEvents.checkCurrentTimeBeforeTime(TimestampEvents.TIME_CHRISTMAS2010_END)){
                _local3 = Global.world.getObjectsByClass(HolidayTree);
                if (_local3.length > 0){
                    GameTransactionManager.addTransaction(new THolidayCheckPresents(HolidayTree.instance));
                };
                if (TimestampEvents.checkCurrentTimeBeforeTime(TimestampEvents.TIME_CHRISTMAS2010)){
                    if (_arg1){
                        if (((TimestampEvents.hasTimestampEventChanged) && ((Global.player.level >= 2)))){
                            showPreHolidayReminder();
                        };
                    };
                };
            };
        }
        private static function doHolidayStartupChecks():void{
            if (Global.player.inventory == null){
                return;
            };
            var _local1:int = Global.player.inventory.getItemCountByName(Item.HOLIDAY2010_PRESENT_ITEM);
            if (TimestampEvents.checkCurrentTimeBetweenRange(TimestampEvents.TIME_CHRISTMAS2010, TimestampEvents.TIME_CHRISTMAS2010_WARNING)){
                if (_local1 > 0){
                    doHolidayStartDialog();
                };
            };
        }
        private static function doWarningHolidayStartupChecks():void{
            var _local1:int = Global.player.inventory.getItemCountByName(Item.HOLIDAY2010_PRESENT_ITEM);
            if (TimestampEvents.checkCurrentTimeBetweenRange_FirstTimeOnly(TimestampEvents.TIME_CHRISTMAS2010_WARNING, TimestampEvents.TIME_CHRISTMAS2010_END)){
                if (_local1 > 0){
                    if (doesPlayerHaveTree()){
                        doPresentExpireWarning();
                    };
                };
            };
        }
        public static function doesPlayerHaveTree():Boolean{
            if (Global.player.inventory.getItemCountByName(Item.HOLIDAY2010_TREE_ITEM) > 0){
                return (true);
            };
            var _local1:Array = Global.world.getObjectsByClass(HolidayTree);
            if (_local1.length > 0){
                return (true);
            };
            return (false);
        }
        private static function areHolidayQuestsActive():Boolean{
            var _local1:Boolean = Global.questManager.isQuestActive("holiday_tree1");
            var _local2:Boolean = Global.questManager.isQuestActive("holiday_tree2");
            var _local3:Boolean = Global.questManager.isQuestActive("holiday_tree3");
            var _local4:Boolean = Global.questManager.isQuestActive("holiday_tree4");
            return (((((((_local1) || (_local2))) || (_local3))) || (_local4)));
        }
        private static function checkToGiveFreeHolidayTree():void{
            var _local1:int;
            if (Global.player.inventory.verifySingletonItem(Item.HOLIDAY2010_TREE_ITEM)){
                _local1 = Global.player.inventory.getItemCountByName(Item.HOLIDAY2010_PRESENT_ITEM);
                if (_local1 > 0){
                    HolidayTree.instance.trackAction("tree_issued");
                    Global.player.inventory.addSingletonItem(Item.HOLIDAY2010_TREE_ITEM, true);
                    GameTransactionManager.addTransaction(new THolidayIssueFreeTree(HolidayTree.instance));
                };
            };
        }
        private static function forceHolidayQuestWrapUp():void{
            Global.questManager.expireSpecificQuest("holiday_tree1");
            Global.questManager.expireSpecificQuest("holiday_tree2");
            Global.questManager.expireSpecificQuest("holiday_tree3");
            Global.questManager.expireSpecificQuest("holiday_tree4");
            GameTransactionManager.addTransaction(new THolidayTreeRemoveAllQuests(HolidayTree.instance));
            checkToGiveFreeHolidayTree();
        }
        public static function wrapUpHolidayQuests():void{
            if (areHolidayQuestsActive()){
                forceHolidayQuestWrapUp();
            };
            checkToGiveFreeHolidayTree();
        }
        private static function doWrapUpHolidayStartUpChecks():void{
            if (TimestampEvents.checkCurrentTimeAfterOrDuringTime_FirstTimeOnly(TimestampEvents.TIME_CHRISTMAS2010_END)){
                forceHolidayQuestWrapUp();
            };
        }
        public static function doStartupCheck():void{
            if (Global.guide.isActive()){
                return;
            };
            doPreHolidayStartupChecks();
            doHolidayStartupChecks();
            doWarningHolidayStartupChecks();
            doWrapUpHolidayStartUpChecks();
        }

        override public function loadObject(_arg1:Object):void{
            super.loadObject(_arg1);
            this.m_mostPresents = int(_arg1.maxPresents);
        }
        override protected function statsInit():void{
            super.statsInit();
            m_statsName = "holidaytree";
        }
        override protected function getCurrentImage():ItemImageInstance{
            var _local1:String = this.getState();
            var _local2:ItemImageInstance = m_item.getCachedImage(_local1, null, m_direction);
            if (_local2 == null){
                m_item.addEventListener(LoaderEvent.LOADED, this.onContractImageLoaded, false, 0, true);
            };
            return (_local2);
        }
        override public function get isHighlightable():Boolean{
            return (true);
        }
        protected function onContractImageLoaded(_arg1:LoaderEvent):void{
            _arg1.target.removeEventListener(LoaderEvent.LOADED, this.onContractImageLoaded);
            reloadImage();
        }
        public function getTimeState():String{
            if (TimestampEvents.checkCurrentTimeBeforeTime(TimestampEvents.TIME_NONE)){
                return (PRE_HOLIDAY);
            };
            if (TimestampEvents.checkCurrentTimeBetweenRange(TimestampEvents.TIME_NONE, TimestampEvents.TIME_CHRISTMAS2010)){
                return (PRE_HOLIDAY);
            };
            if (TimestampEvents.checkCurrentTimeBetweenRange(TimestampEvents.TIME_CHRISTMAS2010, TimestampEvents.TIME_CHRISTMAS2010_WARNING)){
                return (HOLIDAY);
            };
            if (TimestampEvents.checkCurrentTimeBetweenRange(TimestampEvents.TIME_CHRISTMAS2010_WARNING, TimestampEvents.TIME_CHRISTMAS2010_END)){
                return (HOLIDAY_WARNING);
            };
            if (TimestampEvents.checkCurrentTimeAfterOrDuringTime(TimestampEvents.TIME_CHRISTMAS2010_END)){
                return (POST_HOLIDAY);
            };
            return (POST_HOLIDAY);
        }
        private function GetCurrentPresentLevel():int{
            var _local1:int = Global.player.inventory.getItemCountByName(Item.HOLIDAY2010_PRESENT_ITEM);
            if (_local1 == 0){
                return (NOGIFTS);
            };
            if (_local1 == 1){
                if (this.getTimeState() == PRE_HOLIDAY){
                    return (SMALLGIFTS);
                };
                return (LASTGIFT);
            };
            if (_local1 <= Global.gameSettings.getInt("holiday2010_MediumPresents")){
                return (SMALLGIFTS);
            };
            if (_local1 <= Global.gameSettings.getInt("holiday2010_HighPresents")){
                return (MEDIUMGIFTS);
            };
            return (LARGEGIFTS);
        }
        private function getLargeTreeState():String{
            var _local1:int = this.GetCurrentPresentLevel();
            switch (_local1){
                case NOGIFTS:
                    return (STATE_LARGETREE_NOGIFTS);
                case LASTGIFT:
                    return (STATE_LARGETREE_LASTGIFT);
                case SMALLGIFTS:
                    return (STATE_LARGETREE_SMALLGIFTS);
                case MEDIUMGIFTS:
                    return (STATE_LARGETREE_MEDIUMGIFTS);
            };
            return (STATE_LARGETREE_LARGEGIFTS);
        }
        override public function getState():String{
            return (this.getLargeTreeState());
        }
        override public function getToolTipStatus():String{
            var _local2:int;
            var _local1:String;
            if (((!(Global.isVisiting())) && (!(Global.world.isEditMode)))){
                _local2 = Global.player.inventory.getItemCountByName(Item.HOLIDAY2010_PRESENT_ITEM);
                switch (this.getTimeState()){
                    case PRE_HOLIDAY:
                        if (_local2 < Global.gameSettings.getInt("holiday2010_MaxPresents")){
                            _local1 = ZLoc.t("Main", "PreTreeAction", {count:_local2});
                        } else {
                            _local1 = ZLoc.t("Main", "PreTreeActionFull", {count:_local2});
                        };
                        break;
                    case HOLIDAY:
                    case HOLIDAY_WARNING:
                    case POST_HOLIDAY:
                        if (_local2 > 0){
                            _local1 = ZLoc.t("Main", "DuringTreeAction", {count:_local2});
                        } else {
                            _local1 = ZLoc.t("Main", "PostTreeAction");
                        };
                        break;
                };
            };
            return (_local1);
        }
        override public function onPlayAction():void{
            super.onPlayAction();
            var _local1:int = Global.player.inventory.getItemCountByName(Item.HOLIDAY2010_PRESENT_ITEM);
            if (Global.isVisiting()){
                return;
            };
            if (!hasValidId()){
                showObjectBusy();
                return;
            };
            m_actionMode = PLAY_ACTION;
            switch (this.getTimeState()){
                case PRE_HOLIDAY:
                    if (_local1 < Global.gameSettings.getInt("holiday2010_MaxPresents")){
                        HolidayTree.doPresentViral();
                    } else {
                        return;
                    };
                    break;
                case HOLIDAY:
                case HOLIDAY_WARNING:
                case POST_HOLIDAY:
                    if (_local1 > 0){
                        m_Instance.doPresentUnwrap();
                    } else {
                        HolidayTree.doSpecialMessage();
                    };
                    break;
            };
        }
        override public function getGameModeToolTipAction():String{
            var _local1:String = super.getGameModeToolTipAction();
            var _local2:GameMode = Global.world.getTopGameMode();
            if ((_local2 is GMEditSell)){
                if (this.getTimeState() != POST_HOLIDAY){
                    return (null);
                };
            };
            return (_local1);
        }
        override public function onMenuClick(_arg1:MouseEvent):void{
            var _local2:ContextMenuItem;
            if ((_arg1.target is ContextMenuItem)){
                _local2 = (_arg1.target as ContextMenuItem);
                switch (_local2.action){
                    case SELL_OBJECT:
                        if (this.getTimeState() == POST_HOLIDAY){
                            this.sell();
                        } else {
                            return;
                        };
                        break;
                    default:
                        super.onMenuClick(_arg1);
                };
            };
        }
        public function getHarvestingText():String{
            return (ZLoc.t("Main", "Unwrapping"));
        }
        override public function getActionText():String{
            return (this.getHarvestingText());
        }
        override protected function makeDoobers(_arg1:Number=1):Array{
            return (Global.player.processRandomModifiers(harvestingDefinition, this, true, m_secureRands));
        }
        override public function isVisitorInteractable():Boolean{
            return (false);
        }
        public function doPresentUnwrap():void{
            if (this.m_isHarvesting){
                return;
            };
            this.m_isHarvesting = true;
            var _local1:int = this.GetCurrentPresentLevel();
            Sounds.play("unwrap");
            if (_local1 == LASTGIFT){
                HolidayTree.instance.trackAction("unwrap_last_present");
            } else {
                HolidayTree.instance.trackAction("unwrap_present");
            };
            m_actionQueue.removeAllStates();
            m_actionQueue.addActions(new ActionProgressBar(null, this, ZLoc.t("Main", "Unwrapping"), 3));
        }
        override public function getProgressBarStartFunction():Function{
            return (function ():Boolean{
                return (true);
            });
        }
        override public function getProgressBarCancelFunction():Function{
            return (function ():void{
                m_isHarvesting = false;
            });
        }
        override public function getProgressBarEndFunction():Function{
            return (function ():void{
                var _local1:*;
                m_isHarvesting = false;
                m_doobersArray = Global.player.processRandomModifiers(harvestingDefinition, HolidayTree.instance, true, m_secureRands, "miniboss");
                for each (_local1 in m_doobersArray) {
                    StatsManager.sample(100, StatsKingdomType.GAME_ACTIONS, "event", "reward", _local1[0]);
                };
                Global.world.dooberManager.createBatchDoobers(m_doobersArray, harvestingDefinition, m_position.x, m_position.y);
                GameTransactionManager.addTransaction(new THolidayUnwrapPresents(HolidayTree.instance));
                Global.player.inventory.removeItems(Item.HOLIDAY2010_PRESENT_ITEM, 1);
                m_Instance.reloadImage();
            });
        }
        override public function deferProgressBarToNPC():Boolean{
            return (false);
        }
        override public function getProgressBarOffset():Point{
            if (this.content){
                return (new Point(0, (this.content.height >> 1)));
            };
            return (new Point(0, 0));
        }
        override public function sell():void{
            var _local1:int = this.GetCurrentPresentLevel();
            if (_local1 == NOGIFTS){
                UI.displayMessage(ZLoc.t("Main", "HolidaySellTreeWarning", {coins:getSellPrice()}), GenericPopup.TYPE_YESNO, sellConfirmationHandler);
                return;
            };
            UI.displayMessage(ZLoc.t("Main", "HolidaySellTreePresents", {coins:getSellPrice()}), GenericPopup.TYPE_YESNO, sellConfirmationHandler);
        }

    }
}//package Classes 
